package com.A21.GuoSai.demo.service_entity.test;

import com.A21.GuoSai.demo.service_entity.utils.BpmnUtils;
import io.camunda.zeebe.model.bpmn.BpmnModelInstance;
import io.camunda.zeebe.model.bpmn.instance.ExtensionElements;
import io.camunda.zeebe.model.bpmn.instance.Task;
import io.camunda.zeebe.model.bpmn.instance.zeebe.ZeebeInput;
import io.camunda.zeebe.model.bpmn.instance.zeebe.ZeebeIoMapping;
import io.camunda.zeebe.model.bpmn.instance.zeebe.ZeebeOutput;
import org.junit.jupiter.api.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collection;


public class TryToGetVariables {

    public static String getBpmnString(String str) throws IOException {
        String path="processes/"+str;
        InputStream resourceAsStream = TryToGetData.class.getClassLoader().getResourceAsStream(path);
        BufferedReader reader=new BufferedReader(new InputStreamReader(resourceAsStream));
        String ss;
        String bpmn="";
        while ((ss=reader.readLine()) != null) {
            bpmn = bpmn + ss+"\n";
        }

        return bpmn;
    }

    @Test
    public void getAllInputVariables() throws IOException {
        String bpmnStr=getBpmnString("my3.bpmn");
//        System.out.println(bpmnStr);
        BpmnModelInstance modelInstance=BpmnUtils.getModelInstance(bpmnStr);
        Collection<Task> modelElementsByType = modelInstance.getModelElementsByType(Task.class);
        //遍历所有的task组件，依次获取内部的组件
        for(Task task:modelElementsByType){
            //获取task组件中的extensionElements组件
            Collection<ExtensionElements> extensionElements = task.getChildElementsByType(ExtensionElements.class);
            //遍历所有的ExtensionElements组件，依次获取zeebeIoMapping
            for (ExtensionElements extensionElement:extensionElements) {
                //获取ZeebeIoMapping组件
                Collection<ZeebeIoMapping> childElementsByType = extensionElement.getChildElementsByType(ZeebeIoMapping.class);
                for(ZeebeIoMapping ioMap:childElementsByType){

                    //获取所有的输入input
                    Collection<ZeebeInput> inputs = ioMap.getInputs();
                    for(ZeebeInput input:inputs){
                        System.out.println(input.getTarget()+"="+input.getSource());
                    }

                    //获取所有的输出output
                    Collection<ZeebeOutput> outputs = ioMap.getOutputs();
                    for (ZeebeOutput output : outputs){
                        System.out.println(output.getTarget() + "="+output.getSource());
                    }
                }
            }
        }
    }
}
